package com.wxy.demo.executor;

import cn.hutool.core.date.DateUtil;

import java.util.Date;
import java.util.concurrent.*;

/**
 * <p>
 * 停止子线程
 * </p>
 *
 * @author e-Xiaoyuan.Wang
 * @since 2022/10/10 18:21
 */
public class Demo1 {
    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(1);
        Thread[] t = new Thread[1];
        Future<?> future = service.submit(() -> {
            t[0] = Thread.currentThread();
            while (true) {
                System.out.println(DateUtil.formatDateTime(new Date()));
                Thread.sleep(1000);
            }
//            return 1;
        });
        try {
            Object o = future.get(3, TimeUnit.SECONDS);
            System.out.println(o);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            t[0].stop();
            e.printStackTrace();
        }
    }
}
